package com.xing.serviceedu.controller;


import com.xing.commonutils.R;
import com.xing.servicebase.exceptionhandler.GuliException;
import com.xing.serviceedu.entity.EduChapter;
import com.xing.serviceedu.entity.EduCourse;
import com.xing.serviceedu.entity.chapter.ChapterVo;
import com.xing.serviceedu.entity.vo.CourseInfoVo;
import com.xing.serviceedu.entity.vo.CoursePublishVo;
import com.xing.serviceedu.entity.vo.CourseWebVo;
import com.xing.serviceedu.service.EduChapterService;
import com.xing.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xingfuao
 * @since 2022-01-09
 */
@RestController
@RequestMapping("/serviceedu/edu-course")
@CrossOrigin
@Api("课程信息")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) throws GuliException {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程信息
    @GetMapping("getPublicCourseInfo/{id}")
    public R getPublicCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }


    //课程最终状态
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal"); //设置课程发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表 完善分类
    @GetMapping()
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(@ApiParam(name = "courseId",value = "课程Id",required = true)
                         @PathVariable String courseId){
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVoByCourseId(courseId);
        return R.ok().data("course",courseWebVo).data("chapterVoList",chapterVoList);

    }


}

